package com.ahyesyesteam.aicarfaultrepair.controller;


import com.ahyesyesteam.aicarfaultrepair.pojo.Comment;
import com.ahyesyesteam.aicarfaultrepair.service.ICommentService;
import com.ahyesyesteam.aicarfaultrepair.util.Response;
import com.ahyesyesteam.aicarfaultrepair.vo.ErrorCode;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author SSQ
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    ICommentService commentService;

    @RequestMapping("/test")
    public Response test() {
        commentService.save(new Comment());
        commentService.update(new QueryWrapper<Comment>().eq("id", 2));
        commentService.getById(1);
        commentService.remove(new QueryWrapper<Comment>().eq("id", 1));
        return Response.ok().put("aaaa", new Comment());
    }

    @RequestMapping("/add")
    public Response addComment(@RequestBody Comment comment) {
        Comment comment1 = commentService.addComment(comment);
        return Response.ok().put("data", comment1);
    }

    @RequestMapping("/delete/{comment_id}")
    public Response deleteComment(@PathVariable("comment_id") Long commentId) {
        boolean res = commentService.deleteComment(commentId);
        if (res) {
            return Response.ok();
        } else {
            return Response.error(ErrorCode.NO_PERMISSION.getCode(), ErrorCode.NO_PERMISSION.getMsg());
        }
    }

    @RequestMapping("/like/{comment_id}")
    public Response likeComment(@PathVariable("comment_id") Long commentId) {
        return commentService.likeComment(commentId);
    }

    @RequestMapping("/isLike/{comment_id}")
    public Response isLike(@PathVariable("comment_id") Long commentId) {
        boolean like = commentService.isLike(commentId);
        return Response.ok().put("data", like);
    }


    @RequestMapping("/list/{solution_id}")
    public Response getCommentList(@PathVariable("solution_id") Long solutionId) {
        List<Comment> commentList = commentService.getCommentList(solutionId);
        return Response.ok().put("data", commentList);
    }

}
